package com.gaotai.wangfei.exam.filter.config;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 
 * <p>
 * Description: [拦截器链]
 * </p>
 * 
 * @author <a href="mailto: wangfei@gaotai.cn">王飞</a>
 * @version $Revision$
 */
public class InterceptorChain implements IBasicInterceptor
{
	//拦截器链用来存放所有的拦截器
	private List<IBasicInterceptor> interceptorList;

	//目前走到的第几个拦截器
	private int interceptorListCurrentSize = 0;

	//拦截器的setter与getter方法
	public List<IBasicInterceptor> getInterceptorList()
	{
		return interceptorList;
	}

	public void setInterceptorList(List<IBasicInterceptor> interceptorList)
	{
		this.interceptorList = interceptorList;
	}

	/**
	 * 
	 * <p>
	 * 请求拦截
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @param chain
	 * @throws IOException
	 * @throws ServletException
	 * @author:wangfei 2016-3-17
	 * @update: [updatedate] [changer][change description]
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, InterceptorChain chain) throws IOException,
		ServletException
	{
		//说明没有可以调用的拦截器
		if(interceptorListCurrentSize == interceptorList.size())
		{
			//重置计数器
			interceptorListCurrentSize = 0;
			return;
		}
		interceptorList.get(interceptorListCurrentSize++).doFilter(request, response, chain);
	}

	/**
	 * 
	 * <p>
	 * 暂时无需实现功能的destroy方法
	 * </p>
	 * 
	 * @author:wangfei 2016-3-17
	 * @update: [updatedate] [changer][change description]
	 */
	@Override
	public void destroy()
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * <p>
	 * 暂时无需实现功能的init方法
	 * </p>
	 * 
	 * @author:wangfei 2016-3-17
	 * @update: [updatedate] [changer][change description]
	 */
	@Override
	public void init(String interceptorConfig) throws ServletException
	{
		// TODO Auto-generated method stub
	}
}
